//
//  EMToast.swift
//  feiyu444
//
//  Created by hanweiguang on 2018/11/28.
//  Copyright © 2018年 hanweiguang. All rights reserved.
//

import UIKit

public enum EMToastType {
    case EMToastTypeDefault /** 默认的方式，不管有多少，都正常显示，且不论是否会叠加*/
    case EMToastTypeShowLast /** 只显示最后一个提示，如果之前有提示的，直接隐藏*/
    case EMToastTypeShowFirst /** 如果当前的提示还未消失，就不显示以后的提示*/
    case EMToastTypeQueueShow /** 队列依次显示*/
}

class EMToast: NSObject, EMToastControlDelegate {
    
    /**
     * 单例方式
     */
    static let target: EMToast = {
        let result: EMToast = EMToast.init()
        return result
    }()
    
    private var toastControlList: [EMToastControl] = [EMToastControl]()
    
    public class func sharedInstance() -> EMToast {
        return target
    }
    
    override init() {
        super.init()
    }
    
    /**
     * 采用默认的设置
     */
    public func defaultShow(message: String) {
        self.show(message: message, toastType: .EMToastTypeDefault, topMargin: UIScreen.main.bounds.size.height/4.0*3.0, leftMargin: 20)
    }
    
    /**
     * 自定义相关设置
     */
    public func show(message: String, toastType: EMToastType, topMargin: CGFloat, leftMargin: CGFloat) {
        if message.isEmpty {
            return
        }
        
        let tmpArray: Array<EMToastControl> = self.isExistToast() as! Array<EMToastControl>
        
        if toastType == .EMToastTypeShowFirst && tmpArray.count > 0{
            return
        }
        
        if toastType == .EMToastTypeShowLast && (!tmpArray.isEmpty || !self.toastControlList.isEmpty ){
            for toastControl in tmpArray {
                toastControl.removeFromSuperview()
            }
            for toastControl in self.toastControlList {
                toastControl.removeFromSuperview()
            }
        }
        
        let toastControl: EMToastControl = EMToastControl.init(message: message, leftMinMargin: leftMargin, topMargin: topMargin)
        toastControl.delegate = self
        
        if toastType == .EMToastTypeQueueShow {
            self.toastControlList.append(toastControl)
            if tmpArray.isEmpty {
                self.toastControlList.first?.show()
            }
            return;
        }
        
        toastControl.show()
    }
    
    /**
     * 是否已经有Toast正在显示,如果有，获取所有正在显示的Toast
     */
    private func isExistToast() -> NSArray {
        var muArray: [EMToastControl] = [EMToastControl]()
        
        for subView in (UIApplication.shared.keyWindow?.subviews)! {
            if subView.isKind(of: EMToastControl.classForCoder()) {
                muArray.append(subView as! EMToastControl)
            }
        }
        return muArray as NSArray
    }
    
    /**
     * Toast从父view上面被移除了
     */
    func toastDidHide(toastControl: EMToastControl?) {
        print(toastControl!.description + "消失了~~~")
        if !self.toastControlList.isEmpty && self.toastControlList.contains(toastControl!) {
            self.toastControlList.removeFirst()
        }
        
        if !self.toastControlList.isEmpty {
            self.toastControlList.first?.show()
        }
    }
    
}
